<template>
  <div class='sub-sort'>
    <div class="sort">
      <a href="javascript:;" :class="{'active':obj.sortField==='null'}" @click="change('null')" >默认排序</a>
      <a href="javascript:;" :class="{'active':obj.sortField==='publishTime'}" @click="change('publishTime')" >最新商品</a>
      <a href="javascript:;" :class="{'active':obj.sortField==='orderNum'}" @click="change('orderNum')" >最高人气</a>
      <a href="javascript:;" :class="{'active':obj.sortField==='evaluateNum'}" @click="change('evaluateNum')" >评论最多</a>
      <a href="javascript:;"  @click="change('price')" >
        价格排序
        <i class="arrow up"  :class="{'active':obj.sortMethod==='asc'}"/>
        <i class="arrow down" :class="{'active':obj.sortMethod==='desc'}" />
      </a>
    </div>
    <div class="check">
      <XtxCheckbox @abc='fn'>仅显示有货商品</XtxCheckbox>
      <XtxCheckbox @abc='cn'>仅显示特惠商品</XtxCheckbox>
    </div>
  </div>

        <!-- 加载状态 -->

</template>
<script>

import { reactive, watch } from 'vue'
export default {
  name: 'SubSort',
  setup (props, { emit }) {
    const obj = reactive({ sortField: null, sortMethod: null, inventory: false, onlyDiscount: false })
    const change = (i) => {
      obj.sortField = i
      if (i === 'price') {
        if (obj.sortMethod == null) {
          obj.sortMethod = 'desc'
        } else {
          obj.sortMethod = obj.sortMethod === 'desc' ? 'asc' : 'desc'
        }
      } else {
        obj.sortMethod = null
      }
      console.log(obj)
    }
    const fn = (i) => {
      obj.inventory = i
    }
    const cn = (i) => {
      obj.onlyDiscount = i
    }
    watch(obj, (val) => {
      emit('sort-change', val)
    })

    return { fn, change, obj, cn }
  }
}
</script>
<style scoped lang='less'>
.sub-sort {
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .sort {
    display: flex;
    a {
      height: 30px;
      line-height: 28px;
      border: 1px solid #e4e4e4;
      padding: 0 20px;
      margin-right: 20px;
      color: #999;
      border-radius: 2px;
      position: relative;
      transition: all .3s;
      &.active {
        background: @xtxColor;
        border-color: @xtxColor;
        color: #fff;
      }
      .arrow {
        position: absolute;
        border: 5px solid transparent;
        right: 8px;
        &.up {
          top: 3px;
          border-bottom-color: #bbb;
            &.active {
            border-bottom-color: @xtxColor;
          }
        }
        &.down {
          top: 15px;
          border-top-color: #bbb;
          &.active {
            border-top-color: @xtxColor;
          }
        }
      }
    }
  }
  .check {
    .xtx-checkbox {
      margin-left: 20px;
      color: #999;
    }
  }
}

</style>
